<template>
	<div :style="componentModel.cssStyle" :class="componentModel.cssClass">
		<ComponentFactory :componentModel="childComponentModel" v-for="(childComponentModel) in componentModel.children"
			:key="childComponentModel.id"></ComponentFactory>
	</div>
</template>

<script>
	import {
		getPageSubmitData,
		processResponse
	} from '@/utils/pageUtils.js';
	import ComponentFactory from './ComponentFactory.vue';
	import {
		Dialog
	} from 'vant';
    import utils from '@/utils/utils.js';
	import restUtil from '@/utils/restUtil.js';
	export default {
		name: "PageComponent",
		props: ["pageCode", "pageParams"],
		components: {
			ComponentFactory
		},
		data() {
			return {
				componentModel: {},
				pageParaMap: {},
			}
		},
		inject: ['children'],
		mounted() {
			this.children.push(this);
		},beforeUnmount() {
			utils.removeArrayElement(this.children,this);
		},
		methods: {
			setProp(prop) {
				if(prop["paraList"]!=undefined){
					for(var i=0;i<prop["paraList"].length;i++){
						var name=prop["paraList"][i];
						var value=prop[name];
						this.pageParaMap[name]=value;
					}
				}
			},
			sendRequest(requestConfig) {
				var that = this;
				var data = getPageSubmitData(this, this.children);
				for (var k in data) {
					requestConfig["data"][k] = data[k];
				}
				for(k in this.pageParaMap){
					requestConfig["data"][this.componentModel.id+"_"+k] =this.pageParaMap[k];
				}
				restUtil.postAction(this,"view/processRequest?_code=" + this.componentModel.code,
					requestConfig).then(
					function(response) {
						processResponse(that, that, response.data, that.children);
					});
			},
			navigatePage(pageCode, paraMap, isChildPage) { //search弹出框是子页
				if (isChildPage == undefined) {
					this.children.splice(0, this.children.length);
					this.pageParaMap={};
				}
				var that = this;
				var q = "";
				for (var k in paraMap) {
					q += "&" + k + "=" + paraMap[k];
				}
				restUtil.getAction(this,"view/initPage?_code=" + pageCode + q + "&_c=" + Math.random()).then(
					function(response) {
						if (response.data.code == 1) {
							that.componentModel = response.data.viewSerializeMap;
							that.$nextTick(function() {
								processResponse(that, that, response.data, that.children);
							});
						} else if (response.data.code == 2) {
							that.$router.push("login");
						} else {
							Dialog.alert({
								title: '出错了',
								message: response.data.message,
							}).then(() => {
								// on close
							});
						}
					});
			}
		}
	}
</script>

<style>
</style>
